package com.example.server.service.impl;

import com.example.server.mapper.TestCaseMapper;
import com.example.server.pojo.MyEmployee;
import com.example.server.pojo.PageParam;
import com.example.server.pojo.TestCase;
import com.example.server.service.TestCaseService;
import com.example.server.util.PageUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

@Service
public class TestCaseServiceImpl implements TestCaseService {
	@Autowired
	private TestCaseMapper testCaseMapper;

	@Override
	public List<TestCase> allCase() {
		List<TestCase> list = testCaseMapper.allCase();
		return list;
	}

	@Override
	public Map allCaseList(PageParam pageParam) {
		return PageUtils.getPageResult(getPageInfo(pageParam));
	}

	public PageInfo<TestCase> getPageInfo(PageParam pageParam) {
		Integer currentPageParam = pageParam.getPagenum();
		Integer pageSizeParam = pageParam.getPagesize();
		int currenPage = 1;
		int pageSize = 5;
		if (StringUtils.isEmpty(currentPageParam)) {
			currenPage = 1;
		} else {
			currenPage = currentPageParam;
		}
		if (StringUtils.isEmpty(pageSizeParam)) {
			pageSize = 5;
		} else {
			pageSize = pageSizeParam;
		}
		//分页条件查询参数
//        PageHelper.startPage(currenPage, pageSize);//页数和每页大小
//        List<MyEmployee> myEmployeeList = myEmployeeMapper.allEmpPage();
//        PageInfo<MyEmployee> pageInfo = new PageInfo<MyEmployee>(myEmployeeList);
		// 另一种写法，对集合查询进行分页，需要查询所有集合
		PageInfo<TestCase> pageInfo = PageHelper.startPage(currenPage, pageSize).doSelectPageInfo(() -> allCase());
		return pageInfo;
	}

	@Override
	public int addCase(TestCase testCase) {
		return testCaseMapper.addCase(testCase);
	}
}
